package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.LeaveManagement;
import com.example.poverty.service.LeaveManagementService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 前端 请假单 控制类
 */
@RestController
@RequestMapping("/leave")
public class LeaveManagementController {

    @Autowired
    private LeaveManagementService leaveManagementService;

    /**
     * 添加 请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addLeaveManagement(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name").trim();  //姓名
        String startTime = request.getParameter("startTime").trim();  //开始时间
        String endTime = request.getParameter("endTime").trim();  //结束时间
        String reasonLeave = request.getParameter("reasonLeave").trim();  //请假原因

        // 把时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate1 = new Date();
        Date timeDate2 = new Date();
        try {
            timeDate1 = dateFormat.parse(startTime);
            timeDate2 = dateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        LeaveManagement leaveManagement = new LeaveManagement();
        leaveManagement.setName(name);
        leaveManagement.setStartTime(timeDate1);
        leaveManagement.setEndTime(timeDate2);
        leaveManagement.setReasonLeave(reasonLeave);
        boolean flag = leaveManagementService.insert(leaveManagement);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateLeaveManagement(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String name = request.getParameter("name").trim();  //姓名
        String startTime = request.getParameter("startTime").trim();  //开始时间
        String endTime = request.getParameter("endTime").trim();  //结束时间
        String reasonLeave = request.getParameter("reasonLeave").trim();  //请假原因

        // 把时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate1 = new Date();
        Date timeDate2 = new Date();
        try {
            timeDate1 = dateFormat.parse(startTime);
            timeDate2 = dateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        LeaveManagement leaveManagement = new LeaveManagement();
        leaveManagement.setId(Integer.parseInt(id));
        leaveManagement.setName(name);
        leaveManagement.setStartTime(timeDate1);
        leaveManagement.setEndTime(timeDate2);
        leaveManagement.setReasonLeave(reasonLeave);
        boolean flag = leaveManagementService.update(leaveManagement);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 输出请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteLeaveManagement(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = leaveManagementService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return leaveManagementService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allLeaveManagement", method = RequestMethod.GET)
    public Object allLeaveManagement(HttpServletRequest request) {
        return leaveManagementService.allLeaveManagement();
    }

    /**
     * 根据姓名查询请假单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();  //姓名
        return leaveManagementService.getByName(name);
    }


}
